package com.example.goodpet.chat.fragment;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.example.goodpet.R;
import com.example.goodpet.activity.UserInfoActivity;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.db.DatabaseHelper;
import com.example.goodpet.db.entity.User;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.SecurityUtils;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.modules.chat.EaseInputMenuStyle;
import com.hyphenate.easeui.modules.chat.interfaces.IChatExtendMenu;
import com.hyphenate.easeui.modules.chat.interfaces.IChatPrimaryMenu;
import com.hyphenate.easeui.modules.chat.interfaces.OnRecallMessageResultListener;
import com.hyphenate.easeui.modules.menu.EasePopupWindowHelper;
import com.hyphenate.easeui.modules.menu.MenuItemBean;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ChatFragment extends EaseChatFragment  {
    private Dialog dialog;
    private EaseUser eaUser;


    @Override
    public void initData() {
        super.initData();
        resetChatExtendMenu();

        //获取到菜单输入控件
        IChatPrimaryMenu primaryMenu = chatLayout.getChatInputMenu().getPrimaryMenu();
        if(primaryMenu != null) {
        //设置菜单样式为不可用语音模式
            primaryMenu.setMenuShowType(EaseInputMenuStyle.DISABLE_VOICE);
            primaryMenu.showTextStatus();
        }
    }

    private void resetChatExtendMenu(){
        IChatExtendMenu chatExtendMenu = chatLayout.getChatInputMenu().getChatExtendMenu();
        //chatType
        chatExtendMenu.clear();
        chatExtendMenu.registerMenuItem(com.hyphenate.easeui.R.string.attach_picture, com.hyphenate.easeui.R.drawable.ease_chat_image_selector, com.hyphenate.easeui.R.id.extend_item_picture);
        chatExtendMenu.registerMenuItem(com.hyphenate.easeui.R.string.attach_take_pic, com.hyphenate.easeui.R.drawable.ease_chat_takepic_selector, com.hyphenate.easeui.R.id.extend_item_take_picture);
        //chatExtendMenu.registerMenuItem(com.hyphenate.easeui.R.string.attach_video, com.hyphenate.easeui.R.drawable.em_chat_video_selector, com.hyphenate.easeui.R.id.extend_item_video);
    }

    @Override
    public void onPreMenu(EasePopupWindowHelper helper, EMMessage message) {
        //super.onPreMenu(helper, message);
        if(System.currentTimeMillis() - message.getMsgTime() > 2 * 60 * 1000) {
            helper.findItemVisible(com.hyphenate.easeui.R.id.action_chat_recall, false);
        }
    }

    /**
     * 点击头像跳转
     * @param username
     */
    @Override
    public void onUserAvatarClick(String username) {
        super.onUserAvatarClick(username);
        eaUser = EaseIM.getInstance().getUserProvider().getUser(username);
        if (eaUser==null){
            getUserFromRemote(SecurityUtils.unEncryptPhoneNumber(username));
        }else {
            UserPetInfo userPetInfo = new UserPetInfo();
            userPetInfo.setUserId(Long.parseLong(eaUser.getPhone()));
            userPetInfo.setGender(eaUser.getGender());
            userPetInfo.setNickname(eaUser.getNickname());
            userPetInfo.setPhoneNumber(SecurityUtils.unEncryptPhoneNumber(eaUser.getUsername()));
            userPetInfo.setPortrait(eaUser.getAvatar().substring(Constants.BASE_URL_IMAGE.length()));
            userPetInfo.setSignature(eaUser.getSign());
            Intent intent = new Intent(getActivity(), UserInfoActivity.class);
            intent.putExtra("user", userPetInfo);
            startActivity(intent);
        }
    }

    private void getUserFromRemote(String phone){
        UserService userService = RetrofitSingleton.getRetrofitNetConfig().create(UserService.class);
        userService.getUserByPhone(phone).enqueue(new Callback<ResponseBody<User>>() {
            @Override
            public void onResponse(Call<ResponseBody<User>> call, Response<ResponseBody<User>> response) {
                ResponseBody<User> body = response.body();
                if (body != null) {
                    User user = body.getData();
                    DatabaseHelper.getInstance(getActivity()).getUserDAO().insert(user);
                    UserPetInfo userPetInfo = new UserPetInfo();
                    userPetInfo.setUserId(user.getId());
                    userPetInfo.setGender(user.getGender());
                    userPetInfo.setNickname(user.getNickname());
                    userPetInfo.setPhoneNumber(user.getPhoneNumber());
                    userPetInfo.setPortrait(user.getPortrait());
                    userPetInfo.setSignature(user.getSignature());
                    Intent intent = new Intent(getActivity(), UserInfoActivity.class);
                    intent.putExtra("user", userPetInfo);
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<User>> call, Throwable t) {

            }
        });
    }
}